package com.ttai.presenter.activity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.ttai.model.dao.DBHelper;
import com.ttai.model.net.ResponseInfo;
import com.ttai.presenter.base.BasePresenter;
import com.ttai.ui.activity.User;

import retrofit2.Call;

public class UserPresenter extends BasePresenter {
    User user;

    public UserPresenter(User user) {
        this.user = user;
    }


    @Override
    protected void showError(String message) {
        Toast.makeText(user,message,Toast.LENGTH_SHORT).show();

    }

    @Override
    protected void parseJson(JsonObject json) {
        Toast.makeText(user,"注销成功",Toast.LENGTH_SHORT).show();
        user.finish();
    }

    public void clearTokenAcount() {
        DBHelper dbHelper = DBHelper.getInstance(user);
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("token","");
        db.update("tokenacount",values, null, null);
    }

    public void logOut(String token){
        Call<ResponseInfo> logoutInfo = responseInfoApi.logout(token);
        logoutInfo.enqueue(new CallBackAdapter());
    }

    public String getToken() {
        String myToken = "";
        DBHelper dbHelper = DBHelper.getInstance(user);
        SQLiteDatabase sql=dbHelper.getWritableDatabase();
        Cursor cursorToken=sql.rawQuery("select * from tokenacount",null);
        while (cursorToken.moveToNext()){
            myToken=cursorToken.getString(cursorToken.getColumnIndex("token"));
        }
        cursorToken.close();
        return myToken;
    }
}
